package com.ftg.learn.bus.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.VO.UserRidVo;
import com.ftg.learn.bus.VO.UserRoleNameVo;
import com.ftg.learn.bus.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 人员表 Mapper 接口
 * </p>
 *
 * @author lalala
 * @since 2022-03-07
 */
public interface UserMapper extends BaseMapper<User> {

    /**
     * 查询全部人员，及其角色
     *
     * @return
     */
    @Select("select a.*,c.rname from sys_user as a left join sys_user_role as b on a.uid = " +
            "b.uid left join sys_role as c on b.rid = c.rid")
    List<Map<String, Object>> showAddRole();

    /**
     * 根据用户的姓名进行模糊查询
     * @param name
     * @return
     */
    @Select("select * from sys_user where uname like CONCAT('%',#{name},'%')")
    List<User> showByName(@Param("name") String name);

    /**
     * 使用XML写的sql语句 查询人员信息及其角色。
     *
     * @return
     */
    List<Map<String, Object>> queryAllUsers();

    /**
     * 分页查询人员信息及其角色。
     * @param pagination
     * @return
     */
    @Select("select a.uid,a.account,a.password,a.uname,a.sex,a.age,a.status,a.uphone," +
            "c.rname from sys_user as a " +
            "left join sys_user_role as b on a.uid = b.uid " +
            "left join sys_role as c on b.rid = c.rid where a.status = 0")
    List<UserRoleNameVo> showUserAndRoleName(Page<UserRoleNameVo> pagination);

    /**
     * 按名字模糊查询分页功能带角色。
     * @param pagination
     * @return
     */
    @Select("select a.uid,a.account,a.password,a.uname,a.sex,a.age,a.status,a.uphone," +
            "c.rname from sys_user as a " +
            "left join sys_user_role as b on a.uid = b.uid " +
            "left join sys_role as c on b.rid = c.rid where a.uname like CONCAT('%',#{uname},'%') " +
            "and a.status = 0")
    List<UserRoleNameVo> pageUserAndRoleNameByName(Page<UserRoleNameVo> pagination,@Param("uname") String uname);

    /**
     * 查某个人具有的角色
     *
     * @param uid
     * @return
     */
    @Select("select rid from sys_user_role where uid = #{uid}")
    List<Map<String, Object>> showRoleForUserId(@Param("uid") Long uid);


    /**
     * 对操作的用户删除角色
     *
     * @param uid
     * @return
     */
    @Delete("delete from sys_user_role where uid = #{uid}")
    int delRoleForUserId(@Param("uid") Serializable uid);


    /**
     * 对操作的用户添加新角色
     *
     * @param rid
     * @param uid
     * @return
     */
    @Insert("insert into sys_user_role (rid,uid) values(#{rid},#{uid})")
    int saveRoleForUserId(@Param("rid") Long rid, @Param("uid") Long uid);


    /**
     * 更改user表的修改时间为进行当前修改操作的时间
     * @param uid
     * @return
     */
    @Update("update sys_user set update_time = now() where uid = #{uid}")
    boolean updateLocalTime(Long uid);

    /**
     * 重置当前用户密码为88888888
     * @param uid
     * @param password
     * @return
     */
    @Update("update sys_user set password=#{password} where uid = #{uid}")
    boolean resetPassword(@Param("uid")int uid,@Param("password") String password);


    /**
     * 获取当前所有的角色名称
     * @return
     */
    @Select("select rid,rname from sys_role")
    List<Map<String,Object>> showAllRname();


    /**
     * 分页查询历史人员信息及其角色。
     * @param pagination
     * @return
     */
    @Select("select a.uid,a.account,a.password,a.uname,a.sex,a.age,a.status,a.uphone," +
            "c.rname from sys_user as a " +
            "left join sys_user_role as b on a.uid = b.uid " +
            "left join sys_role as c on b.rid = c.rid where a.status = 1")
    List<UserRoleNameVo> showHistoryUser(Page<UserRoleNameVo> pagination);

}
